package com.wyp168.serviceedu.controller;


import com.wyp168.commonutils.R;
import com.wyp168.serviceedu.client.VodClient;
import com.wyp168.serviceedu.entity.EduVideo;
import com.wyp168.serviceedu.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author wyp168
 * @since 2022-05-04
 */
@Api(tags = "小结控制器")
@RestController
@CrossOrigin
@RequestMapping("/serviceedu/edu-video")
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;
    @Autowired
    private VodClient vodClient;

    //新增小结内容
    @PostMapping("addVideo")
    public R addVideo(
            @ApiParam("小结信息(不带Id为新增)") @RequestBody EduVideo video
            ){
        boolean save = videoService.save(video);

        return R.ok();
    }

    //修改小结内容
    @PostMapping("updateVideo")
    public R updateVideo(
            @ApiParam("小结信息(带Id为修改)") @RequestBody EduVideo video
    ) {
        videoService.updateById(video);

        return R.ok();
    }

    //删除小结
    @DeleteMapping("deleteVideo/{videoId}")
    public R deleteVideo(@ApiParam("小结id") @PathVariable String videoId) {
        EduVideo videoInfo = videoService.getById(videoId);
        String videoSourceId = videoInfo.getVideoSourceId();
        if(videoSourceId != null || videoSourceId.length() != 0) {
            R result = vodClient.deleteVideo(videoSourceId);
            System.out.println(result);
        }
        videoService.removeById(videoId);

        return R.ok();
    }

    //根据Id查询小结信息
    @GetMapping("getVideoById/{videoId}")
    public R getVideoById(@ApiParam("小结Id") @PathVariable String videoId) {
        EduVideo video = videoService.getById(videoId);

        return R.ok().data("videoInfo",video);
    }
}

